<template>
  <div class="contain">
    <!-- 搜索 -->
    <el-form :model="query" ref="queryForm" :inline="true" class="top-search" label-position="left">
      <el-form-item label="房源名" prop="keywords">
        <el-input v-model="query.keywords" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label=" ">
        <el-button type="primary" @click="getList()">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 操作菜单 -->
    <div class="mid-action">
      <div>
        <el-button type="primary" size="mini" @click="creatItem('creat')">添加</el-button>
      </div>
    </div>
    <!-- 表格列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="缩略图" align="center" prop="parkIntroduceImg" width="140px">
        <template slot-scope="scope">
          <el-image :src="scope.row.houseImg" fit="cover" style="width:120px;height:80px;"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="装修情况" align="center" prop="fitmentCase" />
      <el-table-column label="房屋实际面积m²" align="center" prop="houseArea" />
      <el-table-column label="单价/天" align="center" prop="univalenceDay" />
      <el-table-column label="单价/月" align="center" prop="univalenceMonth" />
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="creatItem('edit', scope.row)">编辑</el-button>
          <el-button v-hasPermi="['enterpriseWechat:edit']" size="mini" type="text" @click="deleteItem(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="query.pageNum"
      :limit.sync="query.pageSize"
      @pagination="getList()"
    />
    <!-- 编辑表单 -->
    <el-dialog :title="formTtile == 'creat' ? '添加' : '编辑'" :visible.sync="editFormShow">
      <el-form class="edit_form" :model="form" ref="form" label-width="100px" label-position="right">
        <el-form-item label="园区" prop="gardenId">
          <el-select v-model="gardenValue" value-key="gardenName" placeholder="请选择">
            <el-option
              v-for="item in parkList"
              :key="item.id"
              :label="item.gardenName"
              :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="面积范围" prop="areaId">
          <el-select v-model="areaValue" value-key="scope" placeholder="请选择">
              <el-option
                v-for="item in parkArealist"
                :key="item.id"
                :label="item.scope"
                :value="item">
              </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图片" prop="houseImg">
          <el-upload
            class="avatar-uploader"
            :action="uploadUrl"
            :headers="uploadFileHeader"
            :show-file-list="false"
            :on-success="houseImgUpload">
            <img v-if="form.houseImg" :src="form.houseImg" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="装修情况" prop="fitmentCase">
          <el-input class="input" v-model="form.fitmentCase" placeholder="请输入" clearable  />
        </el-form-item>
        <el-form-item label="房屋实际面积" prop="houseArea">
          <el-input class="input" v-model="form.houseArea" placeholder="请输入" clearable  />
        </el-form-item>
        <el-form-item label="单价/天" prop="univalenceDay">
          <el-input class="input" v-model="form.univalenceDay" placeholder="请输入" clearable  />
        </el-form-item>
        <el-form-item label="单价/月" prop="univalenceMonth">
          <el-input class="input" v-model="form.univalenceMonth" placeholder="请输入" clearable  />
        </el-form-item>
        <el-form-item label=" ">
          <el-button type="primary" @click="submit()">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getHouseList, creatHouse, updateHouse, deleteHouse, getHouseInfo, getParkList, getParkAreaList } from "@/api/eh-api";
import { getToken } from "@/utils/auth";
export default {
  data() {
    return {
        uploadUrl:'',
        // 遮罩层
        loading: false,
        // 总条数
        total: 0,
        // 查询参数
        query: {
            pageNum: 1,
            pageSize: 10,
            keywords: undefined,
            gardenId: undefined,
        },
        list:[],
        editFormShow:false,
        formTtile:'creat',
        form:{
          "areaId": 0,
          "fitmentCase": "",
          "gardenId": 0,
          "houseArea": "",
          "houseImg": "",
          "housingId": 0,
          "univalenceDay": "",
          "univalenceMonth": ""
        },
        gardenValue: '',
        areaValue:'',
        // 园区查询参数
        parkQuery: {
            pageNum: 1,
            pageSize: 30,
            gardenName: undefined,
        },
        parkList:[],
        parkArealist:[],
    };
  },
  created() {
    this.uploadUrl = process.env.VUE_APP_BASE_API + '/wx/appletFeedback/uploadimg'
    this.uploadFile = process.env.VUE_APP_BASE_API + '/common/uploadFile2Cos'
    this.uploadFileHeader = {Authorization : "Bearer " + getToken()}
    this.getList()
  },
  methods: {
    /**
     * 获取列表
     */
    getList() {
      this.loading = true;
      getHouseList(this.query)
        .then(({ rows, total }) => {
          this.list = rows;
          this.total = parseInt(total);
          this.loading = false;
          this.ids = [];
        })
        .catch(() => {
          this.loading = false;
        });
    },

    /**
     * 搜索园区 - 表单远程搜索
     */
    getParkList:function(){
        getParkList(this.parkQuery).then( res => {
          this.parkList = res.rows
          this.gardenValue = res.rows.filter( item => {
            return item.id == this.form.gardenId
          })[0]
          // console.log( this.gardenValue )
        })
    },

    /**
     * 获取房源面积列表
     */
    getParkAreaList() {
      getParkAreaList({
        pageNum: 1,
        pageSize: 30
      })
        .then(({ rows, total }) => {
          this.parkArealist = rows;
          this.areaValue = rows.filter( item => {
            return item.id == this.form.areaId
          })[0]
          console.log( this.areaValue )
        })
    },

    /**
     * 上传缩略图
     */
    houseImgUpload:function(res){
      // console.log(res)
      this.form.houseImg = res.data.url
    },

    /**
     * 添加
     */
    creatItem:function(title,item) {
      this.formTtile = title
      this.editFormShow = true
      this.gardenValue = ''
      this.areaValue = ''
      if( title == 'edit' ){ //编辑
        getHouseInfo({
          id: item.id
        }).then( res => {
          if( res.code == 200 ){
            this.form = res.data
            this.form.id = item.id
            this.getParkAreaList() 
            this.getParkList()
          }else{
            this.$message.error(res.msg);
          }
        })
      }else{
        let form = this.form
        Object.keys(this.form).forEach( key => {
          form[key] = ''
        })
        this.form = form
        this.getParkAreaList() 
        this.getParkList()
      }
    },
    /**
     * 提交表单
     */
    submit:function(params) {
      console.log('房源面积',this.areaId)
        console.log( this.form )
        this.form.gardenId = this.gardenValue.id
        this.form.areaId = this.areaValue.id
        if( this.formTtile == 'creat' ){
          creatHouse(this.form).then( res => {
            if( res.code == 200 ){
              this.$message({
                message: '添加成功',
                type: 'success'
              });
              this.getList();
            }else{
              this.$message.error(res.msg);
            }
          })
        }else{
          updateHouse(this.form).then( res => {
              if( res.code == 200 ){
                this.$message({
                  message: '编辑成功',
                  type: 'success'
                });
                this.getList();
              }else{
                this.$message.error(res.msg);
              }
            })
        }
        this.editFormShow = false
    },
    /**
     * 删除
     */
    deleteItem:function(rows) {
        console.log(rows)
        deleteHouse({
          id:rows.id
        }).then( res => {
            if( res.code == 200 ){
              this.$message({
                message: '删除成功',
                type: 'success'
              });
              this.getList();
            }else{
              this.$message.error(res.msg);
            }
        })
    },
    /**
     * 统一路由
     */
    route(row) {
      console.log(row)
      this.$router.push({ path: 'creat', query: { id:row.id } })
    },
  },
};
</script>

<style lang="scss" scoped>
.mid-action {
  display: flex;
  justify-content: space-between;
  margin: 10px 0;
  align-items: center;
  .total {
    background-color: rgba(65, 133, 244, 0.1);
    border: 1px solid rgba(65, 133, 244, 0.2);
    border-radius: 3px;
    font-size: 14px;
    min-height: 32px;
    line-height: 32px;
    padding: 0 12px;
    color: #606266;
  }
  .num {
    color: #00f;
  }
}
.thumbnail {
  width: 160px;
  height: 80px;
}
.input{
  width: 80%;
}

/**头像 */
.avatar-uploader /deep/.el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}
.avatar-uploader /deep/.el-upload:hover {
    border-color: #409EFF;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 128px;
    height: 128px;
    line-height: 128px;
    text-align: center;
}
.avatar {
    width: 128px;
    height: 128px;
    display: block;
}
</style>
